package com.xiaomaoguai.guava.collection;

import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

import org.junit.Test;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
import com.google.common.collect.Multimap;

/**
 * @Description :
 * @FileName: MapTest.java
 * @Author :WeiHui.Zhang
 * @Data : 2015年11月2日 上午9:46:49
 * @Version:V1.00
 */
public class MapTest {

	public static void main(String[] args) {
		// 1.普通的map,key相同的话最后的值会覆盖之前的值
		// 2.HashMap可以key为null，TreeMap不允许
		// 3.HashMap 存放顺序是key的hashCode来的;
		// 4.LinkedHashMap有序
		Map<Integer, String> simpleMap = new HashMap<>();

		simpleMap.put(1, "a");
		simpleMap.put(2, "b");
		simpleMap.put(3, "c");
		simpleMap.put(1, "d");
		simpleMap.put(null, "1");
		simpleMap.put(null, "2");

		System.out.println(simpleMap);

		Map<String, String> simpleMap2 = new HashMap<>();

		simpleMap2.put("1", "a");
		simpleMap2.put("2", "b");
		simpleMap2.put("3", "c");
		simpleMap2.put("4", "d");
		simpleMap2.put(null, "1");
		simpleMap2.put(null, "2");

		System.out.println(simpleMap2);

		Map<Integer, String> linkMap = new LinkedHashMap<>();
		// 有序的map
		linkMap.put(1, "a");
		linkMap.put(2, "b");
		linkMap.put(3, "c");
		linkMap.put(1, "d");
		linkMap.put(null, "1");
		linkMap.put(null, "2");

		System.out.println(linkMap);
	}

	@Test
	public void guavaMap() {
		// 一种key可以重复的map，子类有ListMultimap和SetMultimap，对应的通过key分别得到list和set
		Multimap<Integer, String> map = ArrayListMultimap.create();

		map.put(5, "abc");
		map.put(5, "cdf");

		System.out.println(map);

		Collection<String> collection = map.get(5);
		collection.forEach(action -> System.out.println(action));
	}

	@Test
	public void BiMap() {
		//双向map,是一个一一映射，可以通过key得到value，也可以通过value得到key
		BiMap<Integer,String> biMap=HashBiMap.create();

		biMap.put(1,"hello");
		biMap.put(2,"helloa");
		biMap.put(3,"world");
		biMap.put(4,"worldb");
		biMap.put(5,"my");
		biMap.put(6,"myc");
		
		Integer integer = biMap.inverse().get("my");
		
		System.out.println(integer);

	}
}
